// api/chat_api/chat_remove.go
package chat_api

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	"muziai-server/global"
	"muziai-server/models"
	"muziai-server/utils/jwts"
	"muziai-server/utils/res"
)

func (ChatApi) ChatRemoveView(c *gin.Context) {
	var cr models.IDListRequest
	err := c.ShouldBindJSON(&cr)
	if err != nil {
		res.FailWithError(c, err)
		return
	}
	_claims, _ := c.Get("claims")
	claims := _claims.(*jwts.Claims)
	var query = global.DB.Where("")
	if claims.IsAuth() {
		query.Where("user_id = ?", claims.UserID)
	}

	var chatList []models.ChatModel
	global.DB.Where(query).Find(&chatList, "id in ?", cr.IDList)

	if len(chatList) > 0 {
		global.DB.Delete(&chatList)
	}
	msg := fmt.Sprintf("删除对话 %d 条，成功 %d 条", len(cr.IDList), len(chatList))
	logrus.Infof(msg)

	res.OkWithMsg(c, msg)
}
